package org.senseframework.support.core.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ValueFilter;

/**
 * creator: lanjian
 */
public class JSONUtils {

    public static <T> T parseObject(String json, Class clazz) {
        return (T) JSON.parseObject(json, clazz);
    }

    public static <T> T parseArray(String json, Class clazz) {
        return (T) JSON.parseArray(json, clazz);
    }

    public static <T> T parseArrayNoQ(String json, Class clazz) {
        return (T) JSON.parseArray("["+json+"]", clazz);
    }


    private static ValueFilter filter = new ValueFilter() {
        public Object process(Object obj, String s, Object v) {
            if (v == null || "null".equals(v)) {
                return "";
            }
            return v;
        }
    };

    public static String toJSONString(Object object) {
        return JSON.toJSONString(object, filter, SerializerFeature.WriteMapNullValue);
    }
//    public static String toJSONString(Object object) {
//        return JSON.toJSONString(object, SerializerFeature.WriteNullStringAsEmpty);
//    }

//    public static String toJSONString(Object object, String... sp) {
//        String json = JSON.toJSONString(filter, SerializerFeature.WriteMapNullValue);
//        for (String s : sp) {
//            json = json.replaceAll(s, "");
//        }
//        return json;
//    }

    public static Object parse(String json) {
        return JSON.parse(json);
    }

}
